package com.orhon.smartcampus.modules.teacher.mutation;

import java.util.Date;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import com.alibaba.fastjson.JSON;
import com.orhon.smartcampus.auth.JwtUtils;
import com.orhon.smartcampus.basicComponents.Result;
import com.orhon.smartcampus.modules.base.dao.SchoolsDao;
import com.orhon.smartcampus.modules.teacher.dao.ProfessionalExperienceDao;
import com.orhon.smartcampus.modules.teacher.dao.TInformationDao;
import com.orhon.smartcampus.modules.teacher.entity.ProfessionalExperience;
import com.orhon.smartcampus.modules.teacher.gqlInputModel.GQLProfessionalExperienceInput;
import com.orhon.smartcampus.modules.teacher.inputMapper.ProfessionalExperienceInputMapper;
import com.orhon.smartcampus.modules.user.dao.TokenService;

import graphql.schema.DataFetcher;
import graphql.schema.DataFetchingEnvironment;

@Component
public class AddProfessionalExperienceMutation implements DataFetcher {

    @Autowired
    ProfessionalExperienceDao professionalExperienceDao;

    @Autowired
    TInformationDao tInformationDao;

    @Autowired
    SchoolsDao schoolsDao;

    @Autowired
    TokenService tokenService;

    /**
     * 添加专业工作经历
     * editor : ths
     */
    @Override
    public Object get(DataFetchingEnvironment environment) throws Exception {

        //String token = (String) SecurityUtils.getSubject().getPrincipal();

        String token = tokenService.getSystemToken();
        Long schoolId = JwtUtils.getSchoolId(token);
        GQLProfessionalExperienceInput input = ProfessionalExperienceInputMapper.MAPPER.mapInput(environment.getArgument("inputData"));

        ProfessionalExperience professionalExperience = null;

        if (input.getId() != null) {
            professionalExperience = professionalExperienceDao.get(ProfessionalExperience.class, input.getId());
            professionalExperience.setUpdatedAt(new Date());
        } else {
            professionalExperience = new ProfessionalExperience();
            professionalExperience.setCreatedAt(new Date());
        }
        if(schoolId != null){

            professionalExperience.setSchool_id(schoolId);
        }
        if(input.getTeacher_id() != null){

            professionalExperience.setTeacher_id(input.getTeacher_id());
        }
        if(input.getTime() != null){

            professionalExperience.setTime(input.getTime());
        }
        if(input.getEntime() != null){

            professionalExperience.setEntime(input.getEntime());
        }
        if(input.getImage() != null){

            professionalExperience.setImage(input.getImage());
        }
        if(input.getShenhe() != null){

            professionalExperience.setShenhe(input.getShenhe());
        }
        if(input.getCommonproject() != null){

            professionalExperience.setCommonproject(JSON.toJSONString(input.getCommonproject()));
        }
        if(input.getMembername() != null){

            professionalExperience.setMembername(JSON.toJSONString(input.getMembername()));
        }
        if(input.getJobcontent() != null){

            professionalExperience.setJobcontent(JSON.toJSONString(input.getJobcontent()));
        }
        try{
            professionalExperienceDao.saveOrUpdate(professionalExperience);
        }
        catch (Exception e){
            return Result.error(500,""+e);
        }
        return Result.ok();
    }
}

